﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using Telerik.Web.UI;
using System.Globalization;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class PR_BudgetManagementTest : System.Web.UI.Page
    {
        private SsMng ssMng = new SsMng();
        int EmpSeq;
        RadComboBox cmbJobSite;
        protected void Page_Load(object sender, EventArgs e)
        {
           // IncludeJava();
            if (!IsPostBack)
            {
                EmpSeq = ssMng.EmpSeqI;
                GetDepartName();
                //bindEmpType();
                bindEmpType1();
                //bindYearCombo();
                bindYearCombo1();
                bindJobSite();
                //FirstLoadDataGrid();
            }
            else
            {
                this.cmbJobSite = (RadComboBox)JobSiteCombo.FindControl("cbJobSite");
	            EmpSeq = 1;// ssMng.EmpSeqI;
            }
            
        }
        //public void IncludeJava()
        //{
        //    HtmlGenericControl script = new HtmlGenericControl();
        //    script.TagName = "script";
        //    script.Attributes.Add("type", "text/javascript");
        //    script.Attributes.Add("src", "../../Scripts/jsfunction.js");
           
        //    Page.Header.Controls.Add(script);

        //}
        private void GetDepartName()
        {
            BudgetManagementTestBLL obj = new BudgetManagementTestBLL();
            lblDepName.Text = obj.GetDepartName(this.EmpSeq);
        }

        private void bindEmpType1()
        {
            BudgetManagementTestBLL obj = new BudgetManagementTestBLL();
            cbEmployeeType.DataSource = obj.GetEmployeeTypes();
            cbEmployeeType.DataTextField = "EMP_TYPE_NAME";
            cbEmployeeType.DataValueField = "EMP_TYPE_SEQ";
            cbEmployeeType.DataBind();
            cbEmployeeType.SelectedIndex = 0;
            cbEmployeeType_SelectedIndexChanged(null, null);
        }

        protected void cbEmployeeType_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            BudgetManagementTestBLL obj = new BudgetManagementTestBLL();
            lblTypeYear.Text = obj.GetYearTypeName(Convert.ToInt32(cbEmployeeType.SelectedValue)) + " : ";

        }

        protected void RadGrid2_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            BudgetManagementTestBLL obj = new BudgetManagementTestBLL();

            if (e.CommandName == "Delete")
            {
                int seq = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["mng_test_seq"]);
                obj.Delete(seq, ssMng.EmpSeqI);
            }
            if (e.CommandName == "InitInsert")
            {                
                int AprSeq = obj.GetAprAppraisalsSeq(Int32.Parse(cbEmployeeType.SelectedValue.ToString()),
                    Int32.Parse(cbYear.SelectedValue.ToString()), Int32.Parse(radPeriod.SelectedValue.ToString()));
                Session["PrmSeq"] = obj.GetPrmPromotionSeq(Int32.Parse(cbEmployeeType.SelectedValue.ToString()),
                    Int32.Parse(cbYear.SelectedValue.ToString()), Int32.Parse(radPeriod.SelectedValue.ToString()));
                this.cmbJobSite = (RadComboBox)JobSiteCombo.FindControl("cbJobSite");
                Session["JobSiteSeq"] = Int32.Parse(this.cmbJobSite.SelectedValue.ToString());
            }
            if (e.CommandName == "btnClick")
            {
                return;
            }
            LoadDataGrid();      
        }
        public void OpenNewWindow(string url)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "newWindow", String.Format("<script>window.open('{0}');</script>", url));
        }

        public void bindYearCombo1()
        {
            int curYear = int.Parse(DateTime.Today.Year.ToString());
            curYear += 543;
            int MinYear = curYear - 10;
            int MaxYear = curYear + 10;
            DataTable dt = new DataTable();
            DataColumn dc = dt.Columns.Add("Name");

            for (int i = MinYear; i <= MaxYear; i++)
            {
                dt.Rows.Add(Convert.ToString(i));
            }
            cbYear.DataSource = dt;
            cbYear.DataBind();
            cbYear.SelectedValue = curYear.ToString();
            cbYear_SelectedIndexChanged(null, null);
        }

        protected void cbYear_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            if (cbYear.SelectedIndex == -1)
            {
                radPeriod.Items.Clear();
                return;
            }
            BudgetManagementTestBLL obj = new BudgetManagementTestBLL();
            DataTable dt = obj.GetAppraisals(Int32.Parse(cbYear.SelectedValue.ToString()));
            if (dt != null && dt.Rows.Count > 0)
            {
                radPeriod.Items.Clear();
                foreach (DataRow cur in dt.Rows)
                {
                    ListItem Li = new ListItem();
                    Li.Text = cur["apr_name"].ToString() + " " + ((DateTime)cur["act_start_date"]).ToString("dd/MM/yyyy", new CultureInfo("th-TH")) + " ถึง " + ((DateTime)cur["act_end_date"]).ToString("dd/MM/yyyy", new CultureInfo("th-TH"));
                    Li.Value = cur["apr_period"].ToString();
                    radPeriod.Items.Add(Li);
                }
                radPeriod.DataBind();
                radPeriod.SelectedIndex = 0;
            }
            else
            {
                radPeriod.Items.Clear();
            }

            dt = obj.GetPromotions(Int32.Parse(cbYear.SelectedValue.ToString()));
            if (dt != null && dt.Rows.Count > 0)
            {
                radPromotion.Items.Clear();
                
                radPromotion.DataSource = dt;
                radPromotion.DataTextField = "prm_name";
                radPromotion.DataValueField = "prm_period";
                radPromotion.DataBind();
                radPromotion.SelectedIndex = 0;
            }
            else
            {
                radPromotion.Items.Clear();
            }
        }

        //private void FirstLoadDataGrid()
        //{
        //    try
        //    {
        //        BudgetManagementTestBLL obj = new BudgetManagementTestBLL();

        //        DataTable dt;

        //        dt = obj.select(null);

        //        RadGrid2.DataSource = dt;
        //        RadGrid2.DataBind();
        //    }
        //    catch
        //    {

        //    }
        //}

        private void LoadDataGrid()
        {
            try
            {
                BudgetManagementTestBLL obj = new BudgetManagementTestBLL();

                DataTable dt;
                this.cmbJobSite = (RadComboBox)JobSiteCombo.FindControl("cbJobSite");

	            dt = obj.select(Int32.Parse(this.cmbJobSite.SelectedValue.ToString()),
	                            Int32.Parse(cbEmployeeType.SelectedValue.ToString()),
	                            Int32.Parse(cbYear.SelectedValue.ToString()),
	                            Int32.Parse(radPromotion.SelectedValue.ToString()), 1);//Int32.Parse(radPeriod.SelectedValue.ToString()));

                RadGrid2.DataSource = dt;
                RadGrid2.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.Top;
                RadGrid2.DataBind();
            }
            catch
            {

            }
        }

        private void bindJobSite()
        {
            cmbJobSite = (RadComboBox)JobSiteCombo.FindControl("cbJobSite");
            JobSiteCombo.ListJobSiteComboBox(SystemInfo._JobsiteInternal);
            BudgetManagementTestBLL obj = new BudgetManagementTestBLL();
            cmbJobSite.SelectedValue = obj.GetJobSiteSeq(this.EmpSeq).ToString();
            JobSiteCombo.DataBind();
        }

        protected void RadGrid2_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            {
                string scr;
                if (e.Item.Cells[10].Text == "1")
                    scr = "<img src='../Images/Buttons/Update.gif'>";
                else
                    scr = "<img src='../Images/Buttons/Cancel.gif'>";
                e.Item.Cells[10].Text = scr;                
            }
        }

        protected void cmdSearch_Click(object sender, EventArgs e)
        {
            BudgetManagementTestBLL obj = new BudgetManagementTestBLL();
	        int AprSeq = obj.GetAprAppraisalsSeq(Int32.Parse(cbEmployeeType.SelectedValue.ToString()),
	                                             Int32.Parse(cbYear.SelectedValue.ToString()), 1); // Int32.Parse(radPeriod.SelectedValue.ToString()));
	        Session["PrmSeq"] = obj.GetPrmPromotionSeq(Int32.Parse(cbEmployeeType.SelectedValue.ToString()),
	                                                   Int32.Parse(cbYear.SelectedValue.ToString()), 1); //Int32.Parse(radPeriod.SelectedValue.ToString()));
            this.cmbJobSite = (RadComboBox)JobSiteCombo.FindControl("cbJobSite");
            Session["JobSiteSeq"] = Int32.Parse(this.cmbJobSite.SelectedValue.ToString());

            LoadDataGrid();
        }
    }
}
